package com.yotoodesign.dianba.ui;


import android.content.Intent;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.yotoodesign.core.common.BaseActivity;
import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.util.DownloadUtil;
import com.yotoodesign.dianba.util.SingerService;
import com.yotoodesign.dianba.util.SongService;
import com.yotoodesign.dianba.util.Top10SingerService;
import com.yotoodesign.dianba.util.Top10SongService;

public class SplashActivity extends BaseActivity {

	private GlobalApplication application;
	TextView tvLoading;
	
	@Override
	public void initContentView() {
		setContentView(R.layout.splash_window);
	}

	@Override
	public void initViews() {
		application = (GlobalApplication) getApplication();
		
		tvLoading = (TextView)findViewById(R.id.tvLoading); 
		//得到ImageView控件
        ImageView logo = (ImageView) findViewById(R.id.splash_title);
        //使用xml中自定义的动画效果
        Animation logo_animation = AnimationUtils.loadAnimation(SplashActivity.this, 
        		R.anim.push_left_in);
        logo.setAnimation(logo_animation);
        logo_animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				boolean hasSdCard = false;
				String path = "";
				String state = Environment.getExternalStorageState();
				if(state.equals(Environment.MEDIA_MOUNTED)){
					hasSdCard = true;
					// 得到当前SD卡目录
					path = Environment.getExternalStorageDirectory() + "/" + DownloadUtil.savePath;
				}else{
					path = getFilesDir().getAbsolutePath()+"/" + DownloadUtil.savePath;
				}
				
				tvLoading.setVisibility(View.VISIBLE);
				//动画结束后，开始加载数据
				SingerService.getInstance().prepare(path+"/singers.xml");
				SongService.getInstance().prepare(path+"/songs.xml");
				Top10SingerService.getInstance().prepare(path+"/top10singers.xml");
				Top10SongService.getInstance().prepare(path+"/top10songs.xml");
				
				//进入主页面
				gotoMainActivity();
				finish();
			}
		});
	}

	@Override
	public void setValues() {
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	private void gotoMainActivity(){
		Intent intent = new Intent(SplashActivity.this,MainActivity.class);
		startActivity(intent);
	}

}
